<?php
require_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR."component".DIRECTORY_SEPARATOR.'dedecms'.DIRECTORY_SEPARATOR.'dedehttpdown.class.php');
class Helper_Gather extends DedeHttpDown
{
	private $Charset_Original;
	private $Charset_Destination;

	private $Collection;

	function __construct()
	{
		//
	}

	function getValueByRegular($pattern, $replace)
	{
		//正则匹配
		$pattern = explode('[content]', $pattern);
		$pattern = '/'.$pattern[0].'(.*?)'.$pattern[1].'/is';

		preg_match($pattern, $this->m_html, $array_matches);
		$text = trim($array_matches[1]);

		return $this->replaceByRule($replace, $text);
	}

	function callCollection($Collection_id)
	{
		$lib_Collection = Library::instance("Collection");
		$objs_Collection = $lib_Collection->get_info_by_id($Collection_id);
		$obj_Collection = $objs_Collection[0];
		$this->Collection = $obj_Collection;
	}
	
	function getPagination( $url)
	{
		$this->OpenUrl($url);
		$this->GetHtml();
		if($this->Collection->Charset_Original != $this->Collection->Charset_Destination){
			$this->m_html = iconv($this->Collection->Charset_Original, $this->Collection->Charset_Destination.'//ignore', $this->m_html);
		}

		return $this->getAll($url, $this->Collection->Pattern_Page_Area, $this->Collection->Pattern_Page_Area_Type, $this->Collection->Pattern_Page, $this->Collection->Pattern_Page_AddHome);
	}

	function page($array_pages)
	{
		$array_matches = array();

		foreach($array_pages as $key=>$val){
			$this->OpenUrl($val);
			$this->GetHtml();
			if($this->Collection->Charset_Original != $this->Collection->Charset_Destination){
				$this->m_html = iconv($this->Collection->Charset_Original, $this->Collection->Charset_Destination.'//ignore', $this->m_html);
			}
			$array_matches = array_merge($array_matches, $this->getAll($val, $this->Collection->Pattern_Link_Area, $this->Collection->Pattern_Link_Area_Type, $this->Collection->Pattern_Link, $this->Collection->Pattern_Link_AddHome));
		}
		return $array_matches;
	}

	function getAll($url, $Pattern_Area, $Pattern_Type, $Pattern, $append_url = false)
	{
		$text = $this->getValueByRegular($Pattern_Area, $Pattern_Type);

		//正则匹配
		$pattern = explode('[content]', $Pattern);
		$pattern = '/'.$pattern[0].'(.*?)'.$pattern[1].'/i';
		preg_match_all($pattern, $text, $array_matches);

		if( $append_url ){
			$relative_url = dirname($url);

			if($array_matches){
				foreach($array_matches[1] as $key=>$val){
					$array_matches[1][$key] = $relative_url.'/'.trim($val, '/');
				}
			}
		}
		return $array_matches[1];
	}



	function getValueByString($pattern, $replace)
	{
		//正则匹配
		$pattern = explode('[content]', $pattern);

		$position_start = @strpos($this->m_html, $pattern[0]);
		if( $position_start == false ){
			return '';
		}

		$position_end = @strpos($this->m_html, $pattern[1]);
		if( $position_end == false || $position_end<$position_start){
			return '';
		}

		$position_start += strlen($pattern[0]);

		$text = substr($this->m_html, $position_start, $position_end-$position_start);
		return $this->replaceByRule($replace, $text);
	}

	private function replaceByRule($replace, &$string)
	{
		if( $replace == '' || count($replace) <= 0 ){
			return $string;
		}
		$text = $string;
		foreach($replace as $replace_item)
		{
			if($replace_item[2] == 1){
				$text = preg_replace("/".$replace_item[0]."/is", $replace_item[1], $text);
			} else if($replace_item[2] == 2){
				$text = str_replace($replace_item[0], $replace_item[1], $text);
			}
		}

		return $text;
	}

	public function getSQL(&$array_fields_values, &$array_fields_values_append)
	{
		$fields = "";
		$values = "";
		$steps = 1;
		$steps_count = count($array_fields_values);

		foreach($array_fields_values as $key=>$val){
			$fields .= '`'.$key.'`';
			$values .= "'".addslashes($val)."'";
			if($steps != $steps_count){
				$fields .= ',';
				$values .= ',';
			}
			$steps++;
		}

		$steps_count = count($array_fields_values_append);

		foreach($array_fields_values_append as $key=>$val){
			$fields .= ',`'.$key.'`';
			$values .= ",'".addslashes($val)."'";
		}

		return "insert into {$this->Collection->TableName}($fields)
				values($values)";
	}

	function open($url)
	{
		$this->OpenUrl($url);
		$this->GetHtml();
		//die(var_dump( $this->m_html));
		//var_dump($this->Collection->Charset_Original.$this->Collection->Charset_Destination);
		//die('');
		if($this->Collection->Charset_Original != $this->Collection->Charset_Destination){
			$this->m_html = iconv($this->Collection->Charset_Original, $this->Collection->Charset_Destination.'//ignore', $this->m_html);
		}

		return $this->getValues();
	}

	function getValues()
	{
		$lib_CollectionField = Library::instance("CollectionField");
		$objs_CollectionField = $lib_CollectionField->getList(" and Collection_id='{$this->Collection->id}'");

		$array_field = array();
		$lib_CollectionFieldReplace = Library::instance("CollectionFieldReplace");
		foreach($objs_CollectionField as $obj_CollectionField){
			if($obj_CollectionField->Type == 1){
				$func = "getValueByRegular";
			} else {
				$func = "getValueByString";
			}
			//
			$array_replace = array();
			$objs_CollectionFieldReplace = $lib_CollectionFieldReplace->getList(" and Collection_Field_id='{$obj_CollectionField->id}'");
			foreach($objs_CollectionFieldReplace as $obj_CollectionFieldReplace){
				$array_replace[] = array($obj_CollectionFieldReplace->Pattern, $obj_CollectionFieldReplace->ReplaceString, $obj_CollectionFieldReplace->Type);
			}
			$array_field[$obj_CollectionField->Name] = $this->$func($obj_CollectionField->Pattern, $array_replace);
		}

		return $array_field;
	}
}
?>